function errorToast(text, showtime) {
    console.log('step into errorToast()')
    if (text == null || text == "" || text == undefined) {
        text = "服务器异常，请稍后重试！";
    }
    if (showtime == null || showtime == "" || undefined) {
        showtime = 3;
    }
    showtime = showtime * 1000;
    $(".toast_error").html(text).fadeIn(300);

    setTimeout(function() {
        $(".toast_error").fadeOut(300);
    }, showtime + 300);
}

function getToken() {

}

//获取验证码倒计时
function setTime(dom, countdown, callback) {
    if (countdown == 0) {
        dom.html("重新获取");
        if (callback) {
            callback();
        }
    } else {
        dom.html(countdown + " 秒");
        countdown--;
        setTimeout(function() {
            setTime(dom, countdown, callback);
        }, 1000);
    }
}

/**
 * [tokenVerify description]
 * @return {[type]} [description]
 */
function tokenVerify() {
    try {
        var access_token = getCookie("access_token");
        if (access_token == null || access_token == "" || access_token == undefined) {
            return false;
        }
        return access_token;
    } catch (e) {
        return false;
    }

}

function cardNoVerify() {
    try {
        var targetCardNo = sessionStorage.getItem("targetCardNo");
        if (targetCardNo == null || targetCardNo == "" || access_token == undefined) {
            return false;
        }
        return targetCardNo;
    } catch (e) {
        return false;
    }

}

/**
 * 跳转到登录页
 * @return {[type]} [description]
 */
function goToLogin() {
    window.location.href = "user/login";
}

// 解绑
function unBindCard(oid,fromPage, callback) {
    console.log('step into unBindCard()');
    var url = "wxmp/rest/yellow/exitGroup"
    if(fromPage == "inGroup"){
        var url = "wxmp/rest/group/exitGroup"
    }
    var data = {
        oid: oid
    };

    $(".loading").show();

    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        data: data,
        cache: false,
        timeout: 15000,
        success: function(r) {
            console.log(r);
            $(".loading").hide();

            if (r.code != "00") {
                errorToast(r.msg, 3);
                return false;
            }
            if (callback && typeof(callback) == "function") {
                callback();
            }
        },
        error: function(e) {
            $(".loading").hide();
            ajaxErrorEvent(e);

        }
    });
}

/*
弹出带/不带确定按钮的提示框
参数说明：hasBtn：是否需要显示按钮；msg：提示语；clickEvent：按钮的点击事件
调用例子：   alert_LHX({
                hasBtn: 1,
                msg:"处理异常，请稍后重试",
                clickEvent:function(){
                    xxxx;
                }
            })
 */
function alert_LHX(opts) {
    var hasBtn = 1;
    var msg = "处理异常，请稍后重试";
    var clickEvent = function() {};
    if (!opts) {
        opts = {};
    }

    if (opts.hasBtn || opts.hasBtn == 0) {
        hasBtn = opts.hasBtn;
    }

    if (opts.msg) {
        msg = opts.msg;
    }
    if (opts.clickEvent && typeof(opts.clickEvent) == "function") {
        clickEvent = opts.clickEvent;
    }
    $(".mask_layer > div").addClass('none');
    $(".mask_layer .alert-box-with-btn, .mask_layer .alert-box-without-btn").remove();
            // "<img src='img/fail-bg.jpg'>" +
    var hasBtnText = "<div class='alert-box-with-btn'>" +
        "<h3 class='text-box'></h3>" +
        "<p class='alert-box-btn'>确定</p></div>";
    var noBtnText = "<div class='alert-box-without-btn'>" +
        "<h3 class='text-box'></h3></div>";
    var hasTwoBtnText = "<div class='alert-box-with-two-btn'>" +
        "<h3 class='text-box'>处理异常，请稍后重试</h3>" +
        "<p class='alert-box-btn2'><span class='alert-box-btn-left'>取消</span><span class='alert-box-btn-right'>确定</span></p></div>";
    if (hasBtn == 2) {
        $(".mask_layer").append(hasTwoBtnText).removeClass('none');

        // 点击取消按钮
        $(".alert-box-btn-left").click(function() {
            $(".mask_layer, .mask_layer > div").addClass('none');
        });

        // 点击确定按钮
        $(".alert-box-btn-right").click(function() {
            try {
               ble.maskBoxShow("debbting_box");

            } catch(e) { }
            
            clickEvent();
            //$(".mask_layer, .mask_layer > div").addClass('none');
        });

    } else if (hasBtn == 1) {
        $(".mask_layer").append(hasBtnText).removeClass('none');
        $(".alert-box-btn").click(function() {
            clickEvent();
            $(".mask_layer, .mask_layer > div").addClass('none');
        });
    } else {
        $(".mask_layer").append(noBtnText).removeClass('none');
    }

    $(".text-box").html(msg);
    try {
        if (ble) {
            ble.countover();
        }
    } catch (e) {}
}

function ajaxErrorEvent(e) {
    console.log(e);   
    if (e.statusText == "timeout" || window.navigator.onLine == false) {
        $(".toast_timeout").fadeIn(300, function(e) {
            setTimeout(function() {
                $(".toast_timeout").fadeOut(300);
            }, 3000);
        });
        return false;
    }
    errorToast("", 3);
}